package fr.sigma.labo

import net.sf.ehcache.Ehcache;

import org.springframework.cache.Cache;
import org.springframework.cache.Cache.ValueWrapper;

import grails.plugin.cache.CachePut;
import grails.plugin.cache.Cacheable;
import grails.plugin.cache.ehcache.GrailsEhcacheCache;

class AutoStopService {

	 def grailsCacheManager 
	
	 void putData(String id, Map element) { 
		 Cache cache = grailsCacheManager.getCache('position')
		 cache.put(id, element)
	 }
	 
	 Map getData(String id) {
		Cache cache = grailsCacheManager.getCache('position')
		ValueWrapper value = cache.get(id)
		return value?.get()
	 }
	 
	 List getAllData() {
		 GrailsEhcacheCache cacheg = grailsCacheManager.getCache('position')
		 Ehcache cache = cacheg.nativeCache
		 List<String> ids = cache.getKeys()
		 
		 def datas = []
		 ids.each { String id ->
			 datas += ["id":id,"data":cache.get(id)?.getObjectValue()]
		 }
		 
		 return datas
	  }

}
